//
//  LQYFriendCircleViewModel.m
//  基本框架
//
//  Created by runone02 on 2018/6/28.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "LQYFriendCircleViewModel.h"
#import "LQYCircleItem.h"
#import "LQYSectionHeaderView.h"
#import "NSString+SKYExtension.h"
#import "LQLikeUserCell.h"
@implementation LQYFriendCircleViewModel

- (void)dealloc {
   
}

- (NSMutableArray *)loadDatas {
    NSData *messagesData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"]]];
    NSDictionary *JSONDic = [NSJSONSerialization JSONObjectWithData:messagesData options:NSJSONReadingAllowFragments error:nil];
    NSMutableArray *muArray = [NSMutableArray array];
    for (NSDictionary *eachDic in JSONDic[@"data"][@"rows"]) {
        LQYCircleItem *item = [[LQYCircleItem alloc]initWithItem:eachDic];
        item.nameLabelHeight = SectionHeaderNameLabelHeight;
        item.contentLabelHeight = [self calculateStringHeight:item.message];
        item.imgBgViewHeight = [self getImgBgViewHeight:item];
        [self calculateItemHeight:item];
        [muArray addObject:item];
    }
    return muArray;
}

- (void)calculateItemHeight:(LQYCircleItem *)item {
    if (item.contentLabelHeight == 0) {
        item.headerHeight = SectionHeaderTopSpace + SectionHeaderVerticalSpace + SectionHeaderBottomSpace + item.nameLabelHeight + item.imgBgViewHeight + SectionHeaderTimeLabelHeight;
    } else if (item.contentLabelHeight <= SectionHeaderMaxContentHeight){
        item.headerHeight = SectionHeaderTopSpace + SectionHeaderVerticalSpace * 2 + SectionHeaderBottomSpace + item.nameLabelHeight + item.contentLabelHeight + item.imgBgViewHeight + SectionHeaderTimeLabelHeight;
    } else {
        item.headerHeight = [self getHeaderHeight:item];
    }
    item.likerHeight = [self calculateLikeUserCellHeight:item];
    item.commentHeightArr = [self calculateCellHeight:item];
    if (item.comments.count <= 0 && item.like_users.count <= 0) {
        item.footerHeight = 12 - SectionHeaderBottomSpace - 2;
    } else {
        item.footerHeight = 12;
    }
}

- (CGFloat)calculateLikeUserCellHeight:(LQYCircleItem *)item {
    NSArray *likeUserArr = item.like_users;
    if (item.like_users.count <= 0) {
        return 0;
    }
    NSString *str = [prefixStr copy];
    for (int i = 0; i < likeUserArr.count; i++) {
        NSString *userName = [likeUserArr[i] valueForKey:@"userName"];
        str = [str stringByAppendingString:userName];
        if (i != likeUserArr.count - 1) {
            str = [str stringByAppendingString:@", "];
        }
    }
    return ceil([str contentSizeWithWidth:SCREEN_MIN_LENGTH - (36 + SectionHeaderHorizontalSpace * 2 + 5) - SectionHeaderHorizontalSpace font:[UIFont systemFontOfSize:15] lineSpacing:SectionHeaderLineSpace].height) + 6;
}

- (CGFloat)calculateStringHeight:(NSString *)text {
    return ceil([text contentSizeWithWidth:SCREEN_MIN_LENGTH - SectionHeaderHorizontalSpace * 3 - 36 font:[UIFont systemFontOfSize:SectionHeaderBigFontSize] lineSpacing:SectionHeaderLineSpace].height);
}

- (NSMutableArray *)calculateCellHeight:(LQYCircleItem *)item {
    NSMutableArray *muArr = [NSMutableArray array];
    for (NSDictionary *dic in item.comments) {
        NSString *text = nil;
        if ([dic valueForKey:@"comment_to_user_name"] == nil || [[dic valueForKey:@"comment_to_user_name"] length] <= 0 || [[dic valueForKey:@"comment_to_user_id"] integerValue] == item.user_id) {
            text = [NSString stringWithFormat:@"%@: %@", [dic valueForKey:@"comment_user_name"], [dic valueForKey:@"comment_text"]];
        } else {
            text = [NSString stringWithFormat:@"%@回复%@: %@", [dic valueForKey:@"comment_user_name"], [dic valueForKey:@"comment_to_user_name"], [dic valueForKey:@"comment_text"]];
        }
        CGFloat height = ceil([text contentSizeWithWidth:SCREEN_MIN_LENGTH - (36 + SectionHeaderHorizontalSpace * 2 + 5) - SectionHeaderHorizontalSpace font:[UIFont systemFontOfSize:15] lineSpacing:SectionHeaderLineSpace].height) + 6;
        [muArr addObject:@(height)];
    }
    return muArr;
}

- (CGFloat)getImgBgViewHeight:(LQYCircleItem *)item {
    switch (item.small_pics.count) {
        case 0:
            return 0;
        case 1:
            return SectionHeaderOnePictureHeight + SectionHeaderVerticalSpace;
        case 2:
            
        case 3:
            return SectionHeaderSomePicturesHeight + SectionHeaderVerticalSpace;
        case 4:
            
        case 5:
            
        case 6:
            return SectionHeaderSomePicturesHeight * 2 + SectionHeaderPictureSpace + SectionHeaderVerticalSpace;
        case 7:
            
        case 8:
            
        case 9:
            return SectionHeaderSomePicturesHeight * 3 + SectionHeaderPictureSpace * 2 + SectionHeaderVerticalSpace;
        default:
            return 0;
    }
}

- (CGFloat)getHeaderHeight:(LQYCircleItem *)item {
    CGFloat height = 0;
    if (item.isSpread) {
        height = SectionHeaderTopSpace + SectionHeaderVerticalSpace * 3 + SectionHeaderBottomSpace + item.nameLabelHeight + item.contentLabelHeight + item.imgBgViewHeight + SectionHeaderTimeLabelHeight + SectionHeaderMoreBtnHeight;
    } else {
        height = SectionHeaderTopSpace + SectionHeaderVerticalSpace * 3 + SectionHeaderBottomSpace + item.nameLabelHeight + SectionHeaderMaxContentHeight + item.imgBgViewHeight + SectionHeaderTimeLabelHeight + SectionHeaderMoreBtnHeight;
    }
    return height;
}

@end
